<?php include "../public/header.php";?>
<?php include "../public/header_edit.php";?>
<link rel="stylesheet" type="text/css" href="/static/component/laytp/module/cityPicker/city-picker.css">

<body id="optPage">
<form class="layui-form layui-form-pane" id="commentForm">
    <div class="page-title"><div class="page-title-con"><span>编辑项目开票</span><div class="move_title"></div><?php include "../public/sub.php";?></div></div>
    <div class="main-container">

        <div class="block-generic">
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="edittits"><span>基本信息</span></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>开票登记单号</label>
                        <div class="layui-input-block">
                            <input type="text" id="number" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>项目名称</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="enginid" class="layui-input enginid" onclick="openPage('../public/choose_engin_page_piaokai.php?dom=enginid&page=piao_kai','80%','80%')">
                            <div class="choose_div" id="enginid" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_engin_page_piaokai.php?dom=enginid&page=piao_kai','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">合同名称</label>
                        <div class="layui-input-block">
                            <input type="text" id="shiname" placeholder="请输入" disabled autocomplete="off" class="layui-input disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>开票金额含税(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="money" id="money" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">发票类型</label>
                        <div class="layui-input-block">
                            <select id="piaotype" name="piaotype" lay-filter="piaotype"></select>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>税率(%)</label>
                        <div class="layui-input-block">
                            <input type="number" name="rate" id="rate" placeholder="请输入" clearable autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">税额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="ratefee" id="ratefee" placeholder="自动计算【开票金额含税 /（1+税率）× 税率】" readonly autocomplete="off" class="layui-input moneyinput disabled">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>受票单位</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="duier" class="layui-input duier" onclick="openPage('../public/choose_dealcom.php?dom=duier&page=piao_kai','80%','80%')">
                            <div class="choose_div" id="duier" key="1"></div>
                            <span class="choose_one" onclick="openPage('../public/choose_dealcom.php?dom=duier&page=piao_kai','80%','80%')"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>纳税人识别号</label>
                        <div class="layui-input-block">
                            <input type="text" name="ratenum" id="ratenum" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开户行</label>
                        <div class="layui-input-block">
                            <input type="text" name="bankname" id="bankname" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">账号</label>
                        <div class="layui-input-block">
                            <input type="text" name="banknum" id="banknum" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">地址</label>
                        <div class="layui-input-block">
                            <input type="text" name="unitaddr" id="unitaddr" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="phone" id="phone" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label">施工地点</label>
                        <div class="layui-input-block">
                            <input type="text" name="shidian" id="shidian" placeholder="请输入" clearable  autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label">开票单位</label>
                        <div class="layui-input-block">
                            <div class="tanxian">
                                <div class="tanxianclick" onclick="openPage('../public/choose_userpart.php?dom=kaier&type=dept&dept=chan&single=1','770px','490px');"></div>
                                <div class="tanxiannei" id="kaier"></div>
                            </div>
                            <div class="tancheckdiv"><div class="tancheck" onclick="openPage('../public/choose_userpart.php?dom=kaier&type=dept&dept=chan&single=1','770px','490px');"><i class="layui-icon layui-icon-search"></i></div></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>税费收取方式</label>
                        <div class="layui-input-block">
                            <select id="rateshoutype" name="rateshoutype" lay-filter="rateshoutype" >
                                <option value="">请选择</option>
                                <option value="票前收税">票前收税</option>
                                <option value="票后收税">票后收税</option>
                            </select>
                            <p class="error" id="rateshoutype_error" style="margin: 0;"></p>
                        </div>
                    </div>
                    <div class="layui-col-md6">
                        <label class="layui-form-label"><text>*</text>税费金额(元)</label>
                        <div class="layui-input-block">
                            <input type="text" name="shuijinfee" id="shuijinfee" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item piaoqian layui-hide">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>关联项目收款</label>
                        <div class="layui-input-block">
                            <input type="text" readonly name="incenid" class="layui-input incenid" onclick="choose_income_engin()">
                            <div class="choose_div" id="incenid" key="1"></div>
                            <span class="choose_one" onclick="choose_income_engin()"><i class="layui-icon layui-icon-search"></i></span>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item piaohou layui-hide">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label"><text>*</text>月利率(%)</label>
                        <div class="layui-input-block">
                            <input type="text" name="rateyue" id="rateyue" placeholder="请输入" clearable autocomplete="off" class="layui-input moneyinput">
                        </div>
                    </div>
                </div>
            </div>

            <div class="layui-form-item height_auto">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">
                        <label class="layui-form-label label_textarea">备注</label>
                        <div class="layui-input-block">
                            <textarea id="explain" name="explain" placeholder="请输入" class="layui-textarea"></textarea>
                        </div>
                    </div>
                </div>
            </div>
            <?php include '../public/upinfo.php'?>
        </div>


    </div>
</form>
<script>
    var param_id = getQueryVariable('id') || 0;
    $("#commentForm").validate({
        rules: {
            money: {required: true},
            ratenum: {required: true},
            rateyue: {required: true},
            shuijinfee: {required: true},
        },
        messages:{
            money: {required: "请输入"},
            ratenum: {required: "请输入"},
            rateyue: {required: "请输入"},
            shuijinfee: {required: "请输入"},
        }
    });

    function import_list() {
        openPage('../public/import.php?reveal=piao_kai@importdata', '770px', '490px');
    }


    function choose_income_engin(){
        var enginid = get_choose_val('enginid');
        if(!enginid){
            facade.error("请先选择项目");return false;
        }
        openPage('../public/choose_income_engin.php?dom=incenid&page=piao_kai&enginid='+enginid,'80%','80%')
    }

    layui.use(['laytp', 'form'], function() {
        var form = layui.form, layer = layui.layer;
        render_input_attr();

        facade.ajax({type: 'get', url: ask_piao_kai_edit, data:{id:param_id}}).done(function(res){
            if (res.code === 2000) {
                var data = res.data.data;

                var engininfo = res.data.engininfo;
                if(engininfo){
                    set_choose_btn('enginid',engininfo.id,engininfo.name);
                    $("#shiname").val(engininfo.shiname);
                }

                var kaier_info = res.data.kaier_info;
                if(data.kaier_info.length > 0){
                    tan_tags_init('#kaier', data.kaier_info);
                }


                $("#number").val(data.number);
                $("#money").val(data.money);
                $("#rate").val(data.rate);
                $("#ratefee").val(data.ratefee);
                $("#rateshoutype").val(data.rateshoutype);
                $("#ratenum").val(data.ratenum);
                $("#bankname").val(data.bankname);
                $("#banknum").val(data.banknum);
                $("#unitaddr").val(data.unitaddr);
                $("#phone").val(data.phone);
                $("#shidian").val(data.shidian);
                $("#shuijinfee").val(data.shuijinfee);
                $("#dt").val(data.dt);
                $("#baner").val(data.baner);
                $("#explain").val(data.explain);



                if(data.rateshoutype == '票前收税'){
                    $(".piaoqian").removeClass('layui-hide');
                    $(".piaohou").addClass('layui-hide');

                    var incenidinfo = res.data.incenidinfo;
                    if(incenidinfo){
                        set_choose_btn('incenid',incenidinfo.id,incenidinfo.sericnum);
                    }

                }else  if(data.rateshoutype == '票后收税'){
                    $(".piaoqian").addClass('layui-hide');
                    $(".piaohou").removeClass('layui-hide');
                }else{
                    $(".piaoqian").addClass('layui-hide');
                    $(".piaohou").addClass('layui-hide');
                }


                var piaotype_str = '';
                var piaotype = res.data.piaotype;
                for (var key in piaotype){
                    if(data.piaotype == key){
                        piaotype_str += '<option value="'+key+'" selected>'+piaotype[key]+'</option>';
                    }else{
                        piaotype_str += '<option value="'+key+'">'+piaotype[key]+'</option>';
                    }
                }
                $("#piaotype").html(piaotype_str);


                set_choose_btn('duier',data.duier,data.duier);




                initParseMoney();
                public_upload_file_load('upload_files', res.data.files);
                render_from();
            }
        });

        form.on('select(rateshoutype)', function(data){
            if(data.value == ''){
                $('#rateshoutype_error').show().text('请选择');
                $('#rateshoutype_error').parent().find('input').addClass('select_error');
            }else{
                $('#rateshoutype_error').hide();
                $('#rateshoutype_error').parent().find('input').removeClass('select_error');
            }

            if(data.value == '票前收税'){
                $(".piaoqian").removeClass('layui-hide');
                $(".piaohou").addClass('layui-hide');
            }else  if(data.value == '票后收税'){
                $(".piaoqian").addClass('layui-hide');
                $(".piaohou").removeClass('layui-hide');
            }else{
                $(".piaoqian").addClass('layui-hide');
                $(".piaohou").addClass('layui-hide');
            }
        });

        form.on('submit(add)', function(data){

            data.field.enginid = get_choose_val('enginid');
            data.field.duier = get_choose_val('duier');
            data.field.kaier = tan_tags_getvalue("#kaier", 'dept');
            data.field.incenid = get_choose_val('incenid');

            var validRes = $("#commentForm").valid();

            formValid('enginid');
            if(data.field.enginid == undefined || !data.field.enginid){
                validRes = false;
            }

            formValid('duier');
            if(data.field.duier == undefined || !data.field.duier){
                validRes = false;
            }

            if(data.field.rateshoutype == ''){
                $('#rateshoutype_error').show().text('请选择');
                $('#rateshoutype_error').parent().find('input').addClass('select_error');
                validRes = false;
            }else{
                $('#rateshoutype_error').hide();
                $('#rateshoutype_error').parent().find('input').removeClass('select_error');
            }

            if(data.field.rateshoutype == '票前收税'){
                formValid('incenid');
                if(data.field.incenid == undefined || !data.field.incenid){
                    validRes = false;
                }
                data.field.rateyue = '';
            }else if(data.field.rateshoutype == '票后收税'){
                data.field.incenid = '';
            }else{
                data.field.rateyue = '';
                data.field.incenid = '';
            }



            if(!validRes){return false;}


            data.field.id = param_id;
            data.field.files = public_upload_file_value('upload_files');
            data = parseMoney(data);

            facade.ajax({url: ask_piao_kai_edit, data:data.field}).done(function(res){
                if (res.code === 2000) {
                    dataPostTimeOut();
                }
            });
            return false;
        });

    });



    $("#money, #rate").on('input propertychange',function () {
        jisuan_ratefee();
    });


    //税额算法：=开票金额 /（1+税率）× 税率
    function jisuan_ratefee() {
        var money = parseValidNum($("#money").val() || 0) || 0;
        var rate = parseValidNum($("#rate").val() || 0) || 0;
        var ratefee = money / (1+rate/100) * rate/100;
        $("#ratefee").val(parseFormatNum(ratefee,2));
    }







    function choose_enginid_delete() {
        $("#shiname").val('');
        $("#duier").html('');
        $("#ratenum").val('');
        $("#contract_money").val('');
        $("#bankname").val('');
        $("#banknum").val('');
        $("#unitaddr").val('');
        $("#phone").val('');
    }
</script>
</body>
</html>
